package com.gg.user.controller;

import com.gg.entity.User;
import com.gg.helper.DBHelper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

@WebServlet(name = "LoginServlet", value = "/user/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*1.获取表单用户输入的 emailortel, password*/
        String  emailortel = request.getParameter("emailortel");
        String  password = request.getParameter("password");
        /*
            email:
        * select  * from `user`  where email = ? and `password` = ?
            tel :
            select  * from `user`  where tel = ? and `password` = ?
        * */
        /*2.判断用户到底输入的是email还是手机号  @*/
        /*contains() 是否包含*/
        String sql = "";
        if(emailortel.contains("@")){
            //说明用户输入的是Email
            sql = "select  * from `user`  where email = ? and `password` = ?";
        }else{
            /*输入的是手机号*/
            sql = "select  * from `user`  where tel = ? and `password` = ?";
        }
        //连接数据库
        Connection  con = DBHelper.getConnection();
        //发射器
        try {
            PreparedStatement  ps = con.prepareStatement(sql);
            ps.setString(1,emailortel);
            ps.setString(2,password);
            //执行查询
            ResultSet rs = ps.executeQuery();
            if(!rs.next()){
                /*Email/Tel或者密码错误，请核实！*/
                request.getSession().setAttribute("msg", "Email/Tel或者密码错误，请核实！");
                /*重定向，再回登录页面login.jsp*/
                response.sendRedirect("/fruit/login.jsp");
            }else{
                /*登录成功*/
                /*获取当前系统时间*/
                Date now =   new Date();
                /*把当前时间放到Session里，*/
                request.getSession().setAttribute("currtime", now);

                /*潜规则，把查询回来的满值的User对象放到Session里*/
                String  uid = rs.getString("uid");
                String  username= rs.getString("username");
                password = rs.getString("password");
                String  tel = rs.getString("tel");
                String  email = rs.getString("email");
                //把上面5个值 封装成一个满值User对象
                User u = new User(uid,username,password,tel,email);
                //放到Session里
                request.getSession().setAttribute("user",u);
                /*************************新添加***************************/
                /*新1.  调用查询当前购物车的数量*/
                sql = "SELECT  COUNT(*)  AS cartcount FROM  cart  WHERE uid= ?";
                ps = con.prepareStatement(sql);
                ps.setString(1,uid);
                rs= ps.executeQuery();
                rs.next();
                int cartcount = rs.getInt("cartcount");
                /*新2.   把这个数量放到Session里 */
                request.getSession().setAttribute("cartcount",cartcount);
                /*************************新添加结束***************************/

                //去主页
                response.sendRedirect("/fruit/index.jsp");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
